package com.lam.mall.service.impl;

import java.util.List;
import java.util.Objects;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.lam.common.constant.CommonConstant;
import com.lam.common.utils.StringUtils;
import org.springframework.stereotype.Service;
import com.lam.mall.mapper.AreaMapper;
import com.lam.mall.entity.Area;
import com.lam.mall.param.AreaParam;
import com.lam.mall.service.AreaService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 地区管理Service业务层处理
 * 
 * @author admin
 * @date 2023-04-20
 */
@Service
public class AreaServiceImpl  implements AreaService {

    @Autowired
    private AreaMapper areaMapper;

    /**
     * 查询地区管理
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public Area selectById(String id) {

        return areaMapper.selectById(id);
    }

    /**
     * 查询地区管理列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<Area> selectList(AreaParam param) {
    
        QueryWrapper<Area> queryWrapper = new QueryWrapper<>();

        //关键字查询
        if(StringUtils.isNotBlank(param.getKeywords())) {
            queryWrapper.and(wr->{
                wr.or().like("area_name", param.getKeywords());
            });
        }
        
        queryWrapper.like(StringUtils.isNotBlank(param.getAreaNameKey()), "area_name", param.getAreaNameKey());
        queryWrapper.eq(StringUtils.isNotBlank(param.getAreaName()), "area_name", param.getAreaName());
        queryWrapper.eq(Objects.nonNull(param.getParentId()), "parent_id", param.getParentId());
        queryWrapper.eq(Objects.nonNull(param.getStatus()), "status", param.getStatus());
        queryWrapper.ne(StringUtils.isNotBlank(param.getNeId()), "id", param.getNeId());

        return areaMapper.selectList(queryWrapper);
    }

    /**
     * 新增地区管理
     * 
     * @param area 
     * @return 
     */
    @Override
    @Transactional
    public int insert(Area area) {
    
        return areaMapper.insert(area);
    }

    /**
     * 修改地区管理
     * 
     * @param area 
     * @return 
     */
    @Override
    @Transactional
    public int update(Area area) {
    
        return areaMapper.updateById(area);
    }

    /**
     * 删除地区管理信息
     * 
     * @param idStr ID，多个用逗号分隔
     * @return 
     */
    @Override
    @Transactional
    public int deleteById(String idStr) {
    
        if(StringUtils.isBlank(idStr)) {
            return 0;
        }
        return areaMapper.deleteBatchIds(StringUtils.str2List(idStr));
    }
    
    @Override
    @Cacheable(cacheNames = "area", key = "#pid")
    public List<Area> listByPid(String pid) {
        return areaMapper.selectList(new LambdaQueryWrapper<Area>().eq(Area::getParentId, pid)
        		.eq(Area::getStatus, CommonConstant.STATUS_NORMAL_1));
    }

    @Override
    @CacheEvict(cacheNames = "area", key = "#pid")
    public void removeCacheByPId(String pid) {

    }
    
    
}
